package dd;

import java.util.Scanner;

/*
 * ATM取款机小项目：
 * 功能：
 * 1、帐户管理：登录，查询，修改
 * 	登录：三次机会，
 * 2、资产管理: 查询、取款、存款
 * 
 */
public class D2 {
	public static void main(String[] args) {
		// 帐户数组，放卡号、姓名、密码
		String[][] names = { { "10001", "张三", "123456", }, { "10002", "王五", "234511" }, { "10003", "小明", "332211" } };
		// 资产数组，放存款金额
		double[] money = { 345.5, 22391.5, 10000 };
		Scanner cc = new Scanner(System.in);
		System.out.println("*******欢迎来到闽大学生银行ATM*******");
		int num = 1;
		while (num <= 3) {
			System.out.println("请输入你的卡号：");
			String userCardNo = cc.next();
			System.out.println("请输入你的密码：");
			String userPwd = cc.next();
			// 核对卡号和密码，如果正确就返回下标
			int id = checkLogin(names, userCardNo, userPwd);

			// 根据返回的下标可判断是否登录成功
			if (id != -1) {
				System.out.println("登录成功！欢迎您：" + names[id][1]);
				boolean login = true;
				while (login) {
					System.out.println("请选择你要的操作：" + "\n1、帐户管理" + "\n2、资金管理" + "\n3、退出");
					int b = cc.nextInt();
					if (b == 1) {
						boolean zhang = true;
						while (zhang) {
							System.out.println(
									"******帐户管理中心******" + "\n请选择要对帐户进行的操作" + "\n1.查询账户" + "\n2.修改密码" + "\n3.返回上一级");
							int c = cc.nextInt();
							if (c == 1) {
								System.out.println("您的帐户信息如下：" + "\n 卡号：" + names[id][0] + "\n 姓名：" + names[id][1]
										+ "\n 密码：" + names[id][2]);
							} else if (c == 2) {
								boolean mi = true;
								while (mi) {
									System.out.println("请输入你当前密码：");
									String c1 = cc.next();
									if (checkPwd(names, c1)) {
										System.out.println("请输入新密码：");
										String c2 = cc.next();
										names[id][2] = c2;
										System.out.println("密码修改成功！");
										mi = false;
									} else {
										System.out.println("密码不正确，请重新输入！");
									}
								}

							} else if (c == 3) {
								zhang = false;
							} else {
								System.out.println("选择错误！");
							}
						}
					} else if (b == 2) {
						boolean bs= true;
						while(bs) {
							System.out.println("*******资金管理中心*******" + "\n请选择要对帐户进行的操作" + "\n1.查询余额" + "\n2.取款" + "\n3.存款"
									+ "\n4.返回上一级");
							int b21 = cc.nextInt();
							if(b21==1) {
								System.out.println("您的余额："+money[id]);							
							}else if(b21==2) {
								System.out.println("请输入要取的金额：");
								double b211 = cc.nextDouble();
								System.out.println(getMoney(money,id,b211));
								System.out.println("您的当前余额："+money[id]);
							}else if(b21==3) {
								System.out.println("请输入要存入的金额：");
								double b212 = cc.nextDouble();
								money[id] +=b212;
								System.out.println("您的当前余额："+money[id]);
							}else if(b21==4) {
								bs = false;
							}else{
								System.out.println("选择错误！");
							}
						}
						} else if (b == 3) {
							login = false;
						} else {
							System.out.println("选择错误！");
						}
					}						
				
				break;
			} else {
				System.out.println("卡号或密码错误！");
				if (num < 3) {
					System.out.println("您还有次" + (3 - num) + "机会");
				} else {
					System.out.println("连续三次输入错误，帐号已锁定，请联系发卡银行！");
				}
				num++;

			}

		}System.out.println("欢迎你的使用，再见!");

	}

	// 验证帐号，并把帐号下标返回
	public static int checkLogin(String[][] names, String userCardNo, String userPwd) {
		int ok = -1;
		for (int i = 0; i < names.length; i++) {
			if (names[i][0].equals(userCardNo) && names[i][2].equals(userPwd)) {
				ok = i;
				break;
			}
		}
		return ok;
	}

	// 检查密码
	static boolean checkPwd(String[][] names, String id) {
		boolean x = false;
		for (int i = 0; i < names.length; i++) {
			if (names[i][2].equals(id)) {
				x = true;
			}
		}
		return x;
	}

	// 取款
	public static String getMoney(double[] money, int id, double num) {
		String a = "余额不足，取款失败！";
		if (money[id] >= num) {
			money[id] -= num;
			a = "取款成功";
		}
		return a;
	}


}
